<%--
  Created by IntelliJ IDEA.
  User: wyp
  Date: 2020/5/12
  Time: 8:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <!--引入JQuery，必须要在bootstrap.js之前引入-->
    <script type="text/javascript" src="./resources/js/jquery-3.4.1.min.js"></script>
    <link rel="stylesheet" href="./resources/css/font.css">
    <link rel="stylesheet" href="./resources/css/xadmin.css">
    <!-- <link rel="stylesheet" href="./css/theme5.css"> -->
    <script src="./resources/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./resources/js/xadmin.js"></script>
    <title>Title</title>
</head>
<body>
<script>
    $(document).ready(function () {
        var spotId = 1;//默认当前页为1
        // 获取地址栏id
        var spotId = getQueryVariable("spotId");
        getSpotInfo(spotId);
    });

    // 获取地址栏参数
    function getQueryVariable(variable)
    {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(pair[0] == variable){return pair[1];}
        }
        return(false);
    }

    //根据id查找景点信息
    function getSpotInfo(spotId) {
        $.ajax({
            type: "POST",
            dataType: "json",
            data:{"method":"spotById","spotId":spotId},
            url:"./spotController",
            success:function (data) {
                console.log(data.spotName);
                document.getElementById("spotId").value = data.spotId;
                document.getElementById("spotName").value = data.spotName;
                document.getElementById("spotPlace").value = data.spotPlace;
                document.getElementById("spotItem").value = data.spotItem;
                document.getElementById("spotPrice").value = data.spotPrice;
                document.getElementById("spotOpenTime").value = data.spotOpenTime;
                document.getElementById("spotImgUrl").value = data.spotImgUrl;
            }
        });
    }
    
    //更新景点

    layui.use(['form', 'layer','jquery'],
        function() {
            $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer;


            //监听提交
            form.on('submit(spotUpdate)',
                function(data) {
                    console.log(data);
                    //发异步，把数据提交给后台
                    var spotId = $('#spotId').val();
                    var spotName = $('#spotName').val();
                    var spotPlace = $('#spotPlace').val();
                    var spotItem = $('#spotItem').val();
                    var spotPrice = $('#spotPrice').val();
                    var spotOpenTime = $('#spotOpenTime').val();
                    var spotImgUrl = $('#spotImgUrl').val();
                    console.log("updataSpot测试");

                    $.ajax({
                        type:"POST",
                        dataType: "json",
                        url:"./spotController",
                        data:{"method":"spotUpdate","spotId":spotId,"spotName":spotName,"spotPlace":spotPlace,"spotItem":spotItem,"spotPrice":spotPrice,"spotOpenTime":spotOpenTime,"spotImgUrl":spotImgUrl},
                        success:function(data){
                            layer.alert("更新成功", {
                                    icon: 6
                                },
                                function() {
                                    // 获得frame索引
                                    var index = parent.layer.getFrameIndex(window.name);
                                    //关闭当前frame
                                    parent.layer.close(index);
                                    xadmin.father_reload();
                                });
                        }
                    });
                    return false;
                });

        });
</script>
<%--用来存储spotId--%>
<input type="text" style="display: none" id="spotId" name="spotId">
    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form">
                <div class="layui-form-item">
                    <label for="spotName" class="layui-form-label">
                        <span class="x-red">*</span>景点
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="spotName" name="spotName" required="" lay-verify="required"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="spotPlace" class="layui-form-label">
                        <span class="x-red">*</span>地址
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="spotPlace" name="spotPlace" required="" lay-verify="spotPlace"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="spotItem" class="layui-form-label">
                        <span class="x-red">*</span>游览项目
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="spotItem" name="spotItem" required="" lay-verify="spotItem"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"><span class="x-red">*</span>价格</label>
                    <div class="layui-input-inline">
                        <input type="text" id="spotPrice" name="spotPrice" required="" lay-verify="spotPrice"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="spotOpenTime" class="layui-form-label">
                        <span class="x-red">*</span>开放时间
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="spotOpenTime" name="spotOpenTime" required="" lay-verify="spotOpenTime"
                               autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux">
                        00：00-00:00
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label"><span class="x-red">*</span>图片地址</label>
                    <div class="layui-input-inline">
                        <input type="text" id="spotImgUrl" name="spotImgUrl" required="" lay-verify="spotImgUrl"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>

                <script type="text/javascript">
                    //图片上传
                    layui.use('upload', function(){
                        var $ = layui.jquery
                            ,upload = layui.upload;
                        //普通图片上传
                        var uploadInst = upload.render({
                            elem: '#test1'
                            ,method:'POST'
                            ,accept:'images'
                            ,url: './uploadImages'
                            ,before: function(obj){
                                //预读本地文件示例，不支持ie8
                                obj.preview(function(index, file, result){
                                    $('#previewImg').attr('src', result); //图片链接（base64）
                                });
                            }
                            ,done: function(res){
                                //上传失败
                                if(res.code > 0){
                                    return layer.msg('上传失败');
                                }else {
                                    layer.msg('上传成功');
                                }
                                //上传成功，返回的路径：res.filePath
                                console.log("文件上传路径："+res.filePath);
                                document.getElementById("spotImgUrl").value = res.filePath;

                            }
                            ,error: function(){
                                //上传失败
                                return layer.msg('上传失败，请重试!');
                            }
                        });
                    });

                </script>
                <%--可做成文件上传形式--%>
                <div class="layui-form-item">
                    <label for="spotImgUrl" class="layui-form-label">
                        <span class="x-red">*</span>修改图片
                    </label>
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="test1">上传图片</button>
                        <div class="layui-upload-list ">
                            <img class="layui-upload-img float-right" id="previewImg" style="width: 100px;height: 50px;">
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="spotOpenTime" class="layui-form-label">
                    </label>
                    <button  class="layui-btn" lay-filter="spotUpdate" lay-submit="" >
                        修改
                    </button>
                </div>


            </form>
        </div>
    </div>
</body>
</html>
